package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;

import androidx.annotation.NonNull;

public class QuickLayerDrawable extends LayerDrawable implements QuickDrawable {
    float[] mRadii;
    float mRadius = 0;

    /**
     * Creates a new layer drawable with the list of specified layers.
     *
     * @param layers a list of drawables to use as layers in this new drawable,
     *               must be non-null
     */
    public QuickLayerDrawable(@NonNull Drawable[] layers) {
        super(layers);
    }

    @Override
    public void setCornerRadii(float[] radii) {
        // super.setCornerRadii(radii);
        this.mRadii = radii;
    }

    public float[] getCornerRadii() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            return getCornerRadii();
        } else {
            return null;
        }
    }

    @Override
    public void setCornerRadius(float radius) {
        //super.setCornerRadius(radius);
        mRadius = radius;
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;
    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        mStrokePaint = new Paint();
        mStrokeColors = borderColor;
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(borderWidth);
        mStrokeWidth = borderWidth;
    }

    public float getCornerRadius() {
        return mRadius;
    }

    @Override
    public void draw(Canvas canvas) {
        Rect rect = canvas.getClipBounds();
        RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
        Path path = new Path();
        path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
        if (mRadii != null) {
            path.addRoundRect(rectF, mRadii, Path.Direction.CW);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipOutPath(path);
        }else {
            canvas.clipPath(path);
        }
        super.draw(canvas);

        if(mStrokePaint!=null) {
            canvas.drawPath(path, mStrokePaint);
        }
    }
}
